package scantask.containers;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Vector;

public class PestsLevels {
	
	private HashMap<String, Integer> _levelToValue;
	
	private HashMap<Integer, String> _valueToLevel;
	
	private Vector<Integer> _orderValues;
	
	public PestsLevels() {
		this._levelToValue = new HashMap<String, Integer>();
		this._valueToLevel = new HashMap<Integer, String>();
		
		try {
			FileInputStream fstream = new FileInputStream("resource\\pestsLevels.cfg");
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String line = "";
			while ((line = br.readLine()) != null) {
				String[] lineContent = line.split("=");
				if (lineContent.length != 2) {
					scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Illegal content of the pestsLevels.cfg file");
					scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Illegal content of the pestsLevels.cfg file");
					continue;
				}
				this._levelToValue.put(lineContent[0], new Integer(lineContent[1]));
				this._valueToLevel.put(new Integer(lineContent[1]), lineContent[0]);
			}
		} catch (FileNotFoundException e) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed open the cfg file resource\\pestsLevels.cfg");
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning(e.toString());
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed open the cfg file resource\\pestsLevels.cfg");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning(e.toString());
		} catch (IOException e) {
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning("Failed read line from the cfg file resource\\pestsLevels.cfg");
			scantask.server.utilities.logging.SingletonLogger.getLogger().warning(e.toString());
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning("Failed read line from the cfg file resource\\pestsLevels.cfg");
			scantask.client.utilities.logging.SingletonLogger.getLogger().warning(e.toString());
		}
		
		this._orderValues = new Vector<Integer>(this._valueToLevel.keySet());
		Collections.sort(this._orderValues);
		
	}
	
	public String getLevelOfValue(double val) {
		if (this._orderValues.size() == 0) {
			return "";
		}
		if (this._orderValues.get(0).intValue() >= val) {
			return this._valueToLevel.get(this._orderValues.get(0));
		}
		for (int i = 1; i < this._orderValues.size(); i++) {
			if ((this._orderValues.get(i - 1).intValue() < val) && 
					(this._orderValues.get(i).intValue() >= val)) {
				return this._valueToLevel.get(this._orderValues.get(i));
			}
		}
		return this._valueToLevel.get(this._orderValues.lastElement());
	}
	
	public int getValueOfLevel(String level) {
		Integer val = this._levelToValue.get(level);
		if (val == null) {
			return 0;
		}
		return val.intValue();
	}

}
